﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DataConnector
    {
        /// <summary>
        /// Tạo kết đối bằng 1 đối tượng config.
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static SqlConnection MakeConnection(ConfigDto config)
        {
            String connectionString = "Initial Catalog = " + config.DatabaseName + ";"
                                        + " Data Source = " + config.DataSource + ";"
                                        + " user = " + config.UserName + ";"
                                        + " password = " + config.Password + ";";
            try
            {
                SqlConnection cnn = new SqlConnection(connectionString);
                cnn.Open();
                return cnn;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Tao kết nối bằng config lưu trong file config/config.xml
        /// </summary>
        /// <returns></returns>
        public static SqlConnection MakeConnection()
        {
            ConfigDto config = ConfigDao.ReadConfig();
            return MakeConnection(config);
        }

    }
}
